package com.example.hotel.controller;

import com.example.hotel.common.JsonResult;
import com.example.hotel.entity.Item;
import com.example.hotel.entity.Manager;
import com.example.hotel.service.ManagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/manager")
@Api(tags = "管理员管理", description = "管理员管理相关接口")
public class ManagerController
{

    @Autowired
    private ManagerService managerService;
    @Autowired
    ManagerService service;

    @ApiOperation(value = "获取所有管理员", notes = "返回系统中所有管理员的列表")
    @GetMapping("/findAll")
    public JsonResult<List<Manager>> findAll() {
        List<Manager> list = service.findAll();
        if (!(list.isEmpty())) {
            return JsonResult.ok(list);
        } else {
            return JsonResult.fail(5001, "所有信息查询失败");
        }
    }


    @ApiOperation(value = "添加新管理员", notes = "创建一个新管理员")
    @GetMapping ("/add")
    public JsonResult addManager( Manager manager) {
        try {

            int r = managerService.add(manager);
            if (r > 0) {
                return JsonResult.ok();
            } else {
                return JsonResult.fail(5001, "管理员信息添加失败");
            }

        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail();
        }
    }


    @ApiOperation(value = "根据ID删除管理员", notes = "根据管理员ID删除特定管理员")
    @GetMapping("/delete/{managerId}")
    public JsonResult deleteManager(@PathVariable int managerId) {
        try {
            managerService.deleteById(managerId);
            return JsonResult.ok("删除用户成功，用户ID：" + managerId);
        } catch (Exception e) {
            return JsonResult.fail(404, "删除管理员失败：" + e.getMessage());
        }
    }

    @ApiOperation(value = "更新管理员信息", notes = "通过id更新管理员信息")
    @GetMapping("/update")
    public JsonResult update(
            @Param("manager_id") Integer manager_id,
            @Param("name") String name,
            @Param("sex") String sex,
            @Param("identity_card_number") String identity_card_number,
            @Param("phone") String phone,
            @Param("hire_date") String hire_date, // 接收日期字符串
            @Param("resignation_date") String resignation_date, // 接收日期字符串
            @Param("description") String description) {

        try {
            Manager manager = new Manager();
            manager.setManager_id(manager_id);
            manager.setName(name);
            manager.setSex(sex);

            manager.setIdentity_card_number(identity_card_number);

            manager.setPhone(phone);
            manager.setHire_date(hire_date);
            manager.setResignation_date(resignation_date);
            manager.setDescription(description);

            if (manager != null) {

                int r = service.update(manager); // 假设update方法接受一个Manager对象
                if (r > 0) {
                    return JsonResult.ok();
                } else {
                    return JsonResult.fail(5001, "管理员信息更新失败");
                }
            } else {
                return JsonResult.fail(404, "未找到ID为" + manager_id + "的管理员");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail(500, "更新管理员信息时发生错误：" + e.getMessage());
        }
    }

   }



